package controller;

import command.FormCommand;
import entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.*;

@Controller
@RequestMapping("/form")
public class FormController {

    @RequestMapping("/show")
    public String show(FormCommand formCommand){
        System.out.println("FormController.show()");
        formCommand.setT("abc");
        formCommand.setP("123");
        formCommand.setTa("aaaaaaaa");
        formCommand.setH("111");
        formCommand.setC(Arrays.asList("1","3"));
        formCommand.setCs(new String[]{"1","3"});
        formCommand.setR("2");
        formCommand.setS("2");
        return "form";
    }

    @RequestMapping("/submit")
    public String submit(FormCommand formCommand){
        System.out.println("FormController.submit()");
        System.out.println(formCommand);
        return "redirect:/form/show";
    }

    @ModelAttribute("map")
    public Map loadMap(){
        System.out.println("FormController.loadMap()");
        Map map=new HashMap();
        map.put("1","aaa");
        map.put("2","bbb");
        map.put("3","ccc");
        return map;
    }

    @ModelAttribute("users")
    public List<User> loadUsers(){
        List<User> users=new ArrayList<User>();
        for (int i = 0; i < 10; i++) {
            User user=new User();
            user.setId(i+1);
            user.setUsername("user"+(i+1));
            user.setAge(2*i);
            users.add(user);
        }
        return users;
    }
}
